<template>
  <div class="container">
    <n-drawer v-model:show="isDrawer" :width="width">
      <n-drawer-content :title="title" closable>
        <n-form
          :model="formParams"
          :rules="rules"
          ref="formRef"
          label-placement="left"
          :label-width="100"
        >
          <n-form-item label="设施名称" path="name">
            <n-input placeholder="请输入设施名称" v-model:value="formParams.name" />
          </n-form-item>
          <n-form-item label="设施图标" path="images">
            <el-upload
              class="ml-10 avatar-uploader"
              action=""
              :http-request="upload"
              :show-file-list="false"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="formParams.icon" :src="formParams.icon" class="avatar" />
              <el-icon v-else class="avatar-uploader-icon">
                <Plus />
              </el-icon>
            </el-upload>
          </n-form-item>
        </n-form>

        <template #footer>
          <n-space>
            <n-button type="primary" :loading="subLoading" @click="formSubmit">提交</n-button>
            <n-button @click="handleReset">重置</n-button>
          </n-space>
        </template>
      </n-drawer-content>
    </n-drawer>
  </div>
</template>
<script lang="ts" setup>
  import { defineProps, reactive, ref } from 'vue';
  import { useMessage } from 'naive-ui';
  import { updateProperty } from '@/api/system/property';
  import axios from 'axios';
  import { ElMessage } from 'element-plus';

  const rules = {
    label: {
      required: true,
      message: '请输入标题',
      trigger: 'blur',
    },
    path: {
      required: true,
      message: '请输入路径',
      trigger: 'blur',
    },
  };

  defineProps({
    title: {
      type: String,
      default: '编辑设施',
    },
    width: {
      type: Number,
      default: 450,
    },
  });

  const message = useMessage();
  const formRef: any = ref(null);
  const defaultValueRef = () => ({
    name: '',
    icon: '',
    id: 0,
  });
  let isDrawer = ref(false);
  let subLoading = ref(false);
  let formParams = reactive({
    id: 0,
    name: '',
    icon: '',
  });

  function openDrawer(record) {
    isDrawer.value = true;
    formParams.name = record.name;
    formParams.icon = record.icon;
    formParams.id = record.id;
  }
  defineExpose({
    openDrawer,
  });

  function closeDrawer() {
    isDrawer.value = false;
  }

  const formSubmit = () => {
    formRef.value.validate(async (errors) => {
      if (!errors) {
        let updateUserObj = formParams;
        await updateProperty(updateUserObj);
        message.success('修改成功');
        closeDrawer();
      } else {
        message.error('请填写完整信息');
      }
    });
  };

  function handleReset() {
    formRef.value.restoreValidation();
    formParams = Object.assign(formParams, defaultValueRef());
  }

  function beforeAvatarUpload(rawFile: any) {
    const types = ['image/jpeg', 'image/jpg', 'image/png'];
    const isImage = types.includes(rawFile.type);
    if (!isImage) {
      ElMessage.error('图片必须是jpg或png格式!');
      return false;
    } else if (rawFile.size / 1024 / 1024 > 2) {
      ElMessage.error('图片不能超过2MB!');
      return false;
    }
    return true;
  }

  async function upload(param: any) {
    const formData = new FormData();
    formData.append('file', param.file);
    const url = 'http://localhost:7777/uploadFile';
    await axios
      .post(url, formData)
      .then((data: any) => {
        if (data.data.code == 200) {
          message.success('上传成功');
          formParams.icon = data.data.result;
        }
      })
      .catch((error: any) => {
        console.log(error);
      });
  }
</script>
<style lang="less" scoped></style>
